import time

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as expected_conditions

s = ('https://www.chiphell.com/portal.php')

chrome_path = r'C:\Program Files\Google\Chrome\Application\chromedriver.exe'
chrome_options = webdriver.ChromeOptions()
chrome_options.add_experimental_option('excludeSwitches', ['enable-automation'])
chrome_service = ChromeService(executable_path=chrome_path)

driver = webdriver.Chrome(service=chrome_service, options=chrome_options)

driver.maximize_window()

driver.get('https://www.chiphell.com/portal.php')

# wait_scroll = WebDriverWait(driver,10).until(
#     EC.presence_of_element_located((By.XPATH,'//*[@id="normalthread_33505"]/li/div/a/text()'))
# )

# 获取页面的总高度
last_height = driver.execute_script("return document.body.scrollHeight")

# while True:
#     # 滚动到页面底部
#     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
#
#     # 等待新内容加载
#     time.sleep(2)
#
#     # 获取新的页面高度
#     new_height = driver.execute_script("return document.body.scrollHeight")
#
#     # 如果页面高度没有变化，说明已经到底部
#     if new_height == last_height:
#         break
#
#     last_height = new_height
time.sleep(5)

# view = WebDriverWait(driver,2).until(
#     EC.element_to_be_clickable((By.ID, "autopbn"))
# )
# element = driver.find_element(By.ID,'autopbn')
# time.sleep(2)

# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

# driver.execute_script("arguments[0].scrollIntoView(false);",element)

# driver.execute_script("window.scrollTo(0,document.body.scrollHeight);")

target = WebDriverWait(driver, 3).until(expected_conditions.presence_of_element_located((By.ID, "autopbn")))

for i in range(10):
    if target != None:
        driver.execute_script("arguments[0].scrollIntoView();", target)
        target.click()
        break




# scroll_container = driver.find_element(By.XPATH,"//div[@class='scrollable-container']")
# # 滚动容器内的内容（如向下滚动 300 像素）
# driver.execute_script("arguments[0].scrollTop += 300;", scroll_container)

